<template>
  <el-row>
    <el-page-header @back="goBack" content="修改基本信息">
    </el-page-header>
    <el-col :span="8">
      <div class="grid-content bg-purple">&nbsp;</div>
    </el-col>
    <el-col :span="8">
      <div class="grid-content bg-purple-light">
        <div style="margin-top:50px"></div>
        <el-form label-position="left" label-width="80px" :model="user_t" :rules="rules" ref="user"
        >
          <el-form-item label="姓名" prop="name">
            <el-input v-model="user_t.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="passwd">
            <el-input v-model="user_t.passwd"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="margin-top: 10px" type="primary" @click="submit">修改</el-button>
          </el-form-item>

        </el-form>
      </div>
    </el-col>
    <el-col :span="8">
      <div class="grid-content bg-purple"></div>
    </el-col>
  </el-row>
</template>

<script>
import axios from "axios";
import {mapActions, mapState} from "vuex";

export default {
  name: "updateData",
  data() {
    return {
      user_t: {
        name: '',
        passwd: ''
      },
      path: '',
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        passwd: [
          {required: true, message: '请输入密码号', trigger: 'blur'},
          {min: 6, max: 11, message: '长度应为6到11位', trigger: 'blur'}
        ],
      }
    }
  },
  computed: {
    ...mapState('userOptions', ['user', 'dept', 'token'])
  },
  methods: {
    ...mapActions('userOptions', {getDept: 'findDepts', updateUser: 'updateUser'}),
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goBack() {
      console.log('go back');
      this.$router.push('/Mydata')
    },
    submit() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      axios.put(`/api/admin/admin/`+JSON.stringify({
        name: this.user_t.name,
        passwd:this.user_t.passwd,
        id: this.user.id
      })).then(
          response => {
            if (response.data.code == 200) {
              loading.close();
              this.$message({
                message: '修改成功!',
                type: 'success'
              });
              this.updateUser(this.user_t)
              this.$router.push("/Mydata")

            } else {
              loading.close();
              this.$message.error('服务繁忙');
            }
          },
          error => {
            //请求失败
            loading.close();
            this.$message.error('服务繁忙');
          }
      )
    }
  },
  mounted() {
    this.getDept()
    this.user_t = this.user
  }
}
</script>
<style scoped>
.el-upload__input > input {
  display: none !important;
}

</style>
